﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;
using Zenject;
using System.Collections;
using DG.Tweening;

namespace DiIoc.DemoGame
{
    /// <summary>
    /// 地鼠种类
    /// </summary>
    public enum MoleTypes { None,Good,Bad}

    public class Mole :MonoBehaviour, IPointerClickHandler
    {
        private MoleTypes _moleType;

        /// <summary>
        /// 上升和下降的速度，
        ///TODO：使用GameSetting
        /// </summary>
        private float _upDownTime=1;
        /// <summary>
        /// 停留时间，
        ///TODO：使用GameSetting
        /// </summary>
        private float _stayTime=1;
        /// <summary>
        /// Dotween 动画序列
        /// </summary>
        Sequence upDownTweenSeq;

        /// <summary>
        /// Zenject 信号
        /// </summary>
        private SignalBus _signalBus;

        /// <summary>
        /// 是否存活
        /// </summary>
        private bool alive = true;

        /// <summary>
        /// 方法注入
        /// 注意MemoryPool方式是不允许构造函数传参的，必须重写下面的Reinitialize方法传参
        /// </summary>
        /// <param name="moleType"></param>
        [Inject]
        public void  Construct(SignalBus signalBus)
        {
            _signalBus = signalBus;
            SetTween();
            Reset(MoleTypes.None,Vector3.zero); 
        }
        /// <summary>
        ///设置Dotween动画序列
        /// </summary>
        private void SetTween()
        {
            upDownTweenSeq = DOTween.Sequence();
            upDownTweenSeq.Append(transform.DOMoveY(1, _upDownTime));
            upDownTweenSeq.Append(transform.DOMoveY(-1, _upDownTime)).SetDelay(_stayTime);
            upDownTweenSeq.AppendCallback(() => {
                if (alive)
                {
                    _signalBus.Fire(new MoleSignal { MoleSignalType = MoleSignalTypes.DropDown, Mole = this });
                }
            });
            upDownTweenSeq.SetLoops(-1);
        }
        /// <summary>
        /// 根据类型设置颜色
        /// </summary>
        private void Start()
        {
            var mat = gameObject.GetComponent<Renderer>().material;
            switch (_moleType)
            {
                case MoleTypes.Good:
                    mat.color = Color.green;
                    break;
                case MoleTypes.Bad:
                    mat.color = Color.red;
                    break;
                default:
                    break;
            }
            
        }
        /// <summary>
        /// 对打地鼠动作做出相应
        /// </summary>
        /// <param name="eventData"></param>
        public void OnPointerClick(PointerEventData eventData)
        {
            if (eventData.button == PointerEventData.InputButton.Left)
            {
                alive = false;
                StartCoroutine(Die());
            }
        }
        /// <summary>
        /// 死亡特效
        /// </summary>
        /// <returns></returns>
        private IEnumerator Die()
        {
            //TODO : 实现SoundService类
            //_soundService.PlayFxSound(SoundTyps.MoleHit)
            transform.localScale = new Vector3(1.2f, 0.8f, 1.2f);
            yield return new WaitForSeconds(0.2f);
            transform.localScale = Vector3.zero;
            _signalBus.Fire(new MoleSignal { MoleSignalType = MoleSignalTypes.Hitted, Mole = this });
        }

        /// <summary>
        /// 重置(pool.spawn 方法必须)
        /// </summary>
        /// <param name="moleType"></param>
        private void Reset(MoleTypes moleType,Vector3 pos)
        {
            _moleType = moleType;
            transform.position = pos;
            transform.localScale = Vector3.one;
            if (!upDownTweenSeq.IsPlaying())
            {
                upDownTweenSeq.Play();
            } 
        }
 

        /// <summary>
        /// 使用对象池创建动态物体
        /// </summary>
        public class Pool : MemoryPool<MoleTypes, Vector3, Mole>
        {
            /// <summary>
            /// 重写此方法，重置物体状态
            /// </summary>
            /// <param name="moleType"></param>
            /// <param name="mole"></param>
            protected override void Reinitialize(MoleTypes moleType,Vector3 pos, Mole mole)
            { 
                mole.Reset(moleType, pos);
            }
        } 
    }
}

